package Collection.list;

import java.util.ArrayList;

/**
 * @author yt1105
 * @version 1.0
 */
@SuppressWarnings("all")
public class ListExercise {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Book("红楼梦", "曹雪芹", 20));
        arrayList.add(new Book("西游记", "吴承恩", 2110));
        arrayList.add(new Book("水浒传", "曹雪芹", 2330));
        arrayList.add(new Book("三国演义", "曹雪芹", 220));
        arrayList.add(new Book("猫和老鼠", "JackeyLove", 120));
        bubbleSort(arrayList);
        for (Object o : arrayList) {
            System.out.println(o);
        }


    }

    public static void bubbleSort(ArrayList arrayList) {
        //0~n-1
        //0~n-2
        //0~n-3
        int n = arrayList.size() - 1;
        for (int end = n; end >= 0; end--) {
            for (int i = 1; i <= end; i++) {
                Book book1 = (Book) arrayList.get(i - 1);
                Book book2 = (Book) arrayList.get(i);
                if (book1.getPrice() > book2.getPrice()) {

                    arrayList.set(i, book1);
                    arrayList.set(i - 1, book2);
                    //注意这样写并没有真的交换我们的数据 你只是创建了一个临时变量 没有真的触碰到我们的数据
//                    Book tmp = new Book();
//                    tmp = book1;
//                    book1 = book2;
//                    book2 = tmp;
                }

            }
        }

    }
}


class Book {
    private String name;
    private String author;
    private double price;

    public Book() {
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }
}